<template>
  <van-picker
    title="性别"
    show-toolbar
    :columns="columns"
    @confirm="onConfirm"
    @change="onChange"
    @cancel="$emit('close')"
    :default-index="gender"
  />
</template>

<script>
import { updateUser } from '@/api/user'

export default {
  props: ['value'],
  data () {
    return {
      columns: ['男', '女'],
      gender: this.value
    }
  },
  methods: {
    async onConfirm() {
      try {
        await updateUser({
          gender: this.gender
        })

        this.$emit('close')
        this.$emit('input', this.gender)
      } catch (err) {
        console.log(err)
      }
    },
    onChange(picker, value, index) {
      this.gender = index
    }
  }
}
</script>

<style scoped lang='less'>

</style>
